<template>
  <!-- 用户管理页面组件 -->
  <div class="RootBox">
    <!-- 面包屑导航 -->
    <div class="wanjun">
      <div class="mbnav">
        <div class="mbnavsontwo">
          <!-- 左侧图标 -->
          <el-row class="iconlist">
            <span class="mobile">
              <el-button class="addUser" size="mini" icon="iconfont icon-dingbudaohang-zhangh btnicon" @click="addUser"
                >新建用户</el-button
              >
            </span>
            <span class="mobile">
              <el-button size="mini" class="Refresh" icon="iconfont icon-ico_reset btnicon" @click="RefreshUser"
                >刷新</el-button
              >
            </span>
          </el-row>
          <div class="mobile searchfather">
            <el-input placeholder="通过手机号查询" clearable v-model="userName" class="input-with-select"></el-input>
            <el-input
              placeholder="通过联系人姓名查询"
              clearable
              v-model="linkUserName"
              class="input-with-select"
            ></el-input>
            <el-input
              placeholder="通过单位名称查询"
              clearable
              v-model="organizationName"
              class="input-with-select"
            ></el-input>

            <el-button class="search" size="mini" type="primary" @click="serachResult">搜索</el-button>
            <el-button class="search" size="mini" type="primary" @click="resetResult">重置</el-button>
          </div>
        </div>
      </div>
    </div>
    <!-- 树状图加表格 -->
    <div class="tabbleList">
      <!-- 表单结构图 -->
      <div class="fatherform">
        <div class="tableFather">
          <el-table
            height="540"
            v-if="tableDatarow"
            border=""
            :data="tableDatarow"
            @selection-change="tablerowchange"
            stripe
            style="width: 100%"
            ref="multipleTable"
            size="mini"
            :header-cell-style="{ background: '#eff3f8', color: '#606266' }"
          >
            <!-- <el-table-column type="selection" width="55" :selectable="selectable"></el-table-column> -->
            <!-- <el-table-column prop="sort" sortable label="序号"></el-table-column> -->
            <el-table-column prop="userName" label="电话"> </el-table-column>

            <el-table-column prop="linkUserName" label="联系人姓名"></el-table-column>
            <el-table-column prop="email" label="电子邮箱"></el-table-column>
            <el-table-column prop="organizationName" label="单位名称"></el-table-column>

            <el-table-column prop="organizationType" :formatter="formatter" label="单位类型"></el-table-column>
            <el-table-column prop="sysRoleNames" :formatter="sysRoleNamesFormatter" label="账户类型"></el-table-column>

            <!-- <el-table-column prop="sort" label="用户状态(启用/停用)">
              <template slot-scope="scope">
                <div>
                  <el-switch
                    @change="switchChangeHandler($event, scope.row)"
                    v-model="scope.row.stopFlag"
                    :active-value="0"
                    :inactive-value="1"
                    active-color="#409eff"
                    inactive-color="#dcdfe6"
                  ></el-switch>
                </div>
              </template>
            </el-table-column> -->
            <el-table-column label="操作" width="480">
              <template slot-scope="scope">
                <div style="text-align: left">
                  <el-button
                    size="mini"
                    class="updateUser"
                    @click="updateUser(scope.row)"
                    v-if="scope.row.userName !== 'sys_admin'"
                    >编辑</el-button
                  >
                  <el-button
                    size="mini"
                    type="danger"
                    @click="logoutUser(scope.row)"
                    v-if="scope.row.userName !== 'sys_admin'"
                    >删除</el-button
                  >
                  <el-button
                    size="mini"
                    v-if="scope.row.userName !== 'sys_admin'"
                    class="addUser"
                    @click="ResetPassword(scope.row)"
                    >重置密码</el-button
                  >
                  <!-- <el-button
                    type="primary"
                    size="mini"
                    style="margin-left: 10px"
                    @click="selectLearder(scope.row)"
                    >直属领导</el-button
                  > -->
                </div>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <Pagecomponent
          :total="total"
          ref="pagecom"
          :pagesizelist="[15, 20]"
          @getAdminRative="pageChange"
        ></Pagecomponent>
        <el-dialog title="重置密码" :visible.sync="dialogVisibleUser" width="400px" @closed="clearpsd">
          <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px">
            <el-form-item label="密码" prop="psd">
              <el-input placeholder="请输入密码" v-model="ruleForm.psd" clearable></el-input>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button size="mini" @click="dialogVisibleUser = false">取 消</el-button>
            <el-button size="mini" type="primary" @click="submitpssword('ruleForm')">确 定</el-button>
          </span>
        </el-dialog>
      </div>
      <learder ref="leaderCom" :userRow="userRow"></learder>
    </div>
    <UsreMessageDialog @getList="getUserRelease" :show.sync="showUserDialog" :editUserId="editUserId" />
  </div>
</template>
<script>
import Pagecomponent from '@/components/custom/PageCom.vue'
import { filterListTree, getTreeObjHanlder } from '@/utils/tools/funtctionAPI.js'
import learder from './components/learder.vue'
import UsreMessageDialog from './components/UsreMessageDialog.vue'
import authorityApi from '@/utils/tools/authorityApi/index.js'
import apiConfig from '@/utils/tools/authorityApi/apiConfig'
import { axiosUtil } from '@/utils/tools/authorityApi/axiosUtil'
export default {
  name: 'user',
  components: {
    UsreMessageDialog,
    Pagecomponent,
    learder
  },
  data() {
    return {
      userIDpsd: '',
      input: '', //修改密码输入框
      dialogVisibleUser: false, //修改密码弹框
      editUserId: '',
      userIsEdit: false,
      showUserDialog: false,
      isEdstatusList: [
        {
          name: '启用',
          value: 0
        },
        {
          name: '停用',
          value: 1
        }
      ],
      logoutList: [
        {
          name: '已注销',
          value: 1
        },
        {
          name: '未注销',
          value: 0
        }
      ],
      rules: {
        psd: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 6,
            max: 15,
            message: '长度在 6 到 15 个字符',
            trigger: 'blur'
          }
        ]
      },
      ruleForm: {
        psd: ''
      },
      stopFlag: '',
      deleteFlag: '',
      value: true,
      defaulitOpenList: [], //树形节点记录节点状态
      organizeId: '', //当前选中机构id
      dialogFormVisible: false,
      getUserList: [], // 获取到的树状结构数组
      // falg: true, //默认复选框隐藏
      // 表格数组
      tableDatarow: [],
      // 表单数组
      tableData: new Array(10).fill(),
      // 表头数据
      name: '机构名称', //名称
      sort: '序号', //编码
      formLabelWidth: '120px',
      //   行政级别下拉数据
      options: [],
      // 上级机构级别
      optionslist: [],
      value1: '',
      degPageTypeName: '', //机构名称
      degPageTypeNameId: '', //机构ID
      degPageTypeLs: null,
      degPageTypeDutyId: '', //岗位ID
      dialogImageUrl: '',
      dialogVisible: false,
      currentPage: 1,
      total: 0, //总数
      pageSize: 15, //每页显示多少条
      tablerow: [], //当前选中行
      userName: '', //名称查询
      organizationName: '', //名称查询
      linkUserName: '', //名称查询
      input4: '', //机构查询
      dutyName: '', //岗位查询
      PostList: [], // 岗位列表数据
      userRow: null //当前所选用户信息
    }
  },
  created() {
    this.getUserRelease()
    // this.dialogbtnhandle(true)
  },
  methods: {
    pageChange(list) {
      console.log(list)
      this.getUserRelease(list)
    },
    selectLearder(row) {
      this.userRow = row
      this.$refs.leaderCom.drawer = true
    },
    //组员删除
    deleteGorpMember(row) {
      this.$confirm('此操作将永久删除所有组下该身份成员, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let sysOverallInfo = {
          overallOrganizeId: row.organizeId,
          overallUserID: row.id,
          overallDutyId: row.dutyId
        }
        authorityApi.sysGroupPort.deleteGoupUser({ sysOverallInfo: sysOverallInfo }, (res) => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
      })
    },
    clearpsd() {
      this.ruleForm.psd = ''
      this.$refs.ruleForm.clearValidate()
    },
    // 获取机构列表数据
    // dialogbtnhandle(falg) {
    //   if (falg) {
    //     authorityApi.OrganizationPort.getOrganizationList({}, (res) => {
    //       if (!res) return
    //       this.degPageTypeLs = filterListTree(res)
    //     })
    //   }
    // },
    //状态切换
    switchChangeHandler(newval, oldval) {
      authorityApi.UserPort.alterEnableStatus({ userId: oldval.id, stopFlag: newval }, (res) => {
        //
        this.$message.success(res)
      })
    },
    formatter(row) {
      let str = ''
      // <!-- 0企业 1政府机构 2事业单位 3社会团体 4其他 -->
      switch (row.organizationType) {
        case 0:
          return '企业'

        case 1:
          return '政府机构'

        case 2:
          return '事业单位'

        case 3:
          return '社会团体'

        case 4:
          return '其他'
      }
    },
    sysRoleNamesFormatter(row) {
      if (!row.sysRoleNames) {
        return ''
      } else {
        return row.sysRoleNames.join(',')
      }
    },
    selectable(row) {
      if (row.id !== authorityApi.sysOverallInfo.overallUserID) {
        return true
      } else {
        return false
      }
    },

    handleClears() {
      this.input4 = ''
      this.degPageTypeName = ''
      this.degPageTypeNameId = ''
      this.degPageTypeDutyId = ''
    },
    clearDutyId() {
      this.degPageTypeDutyId = ''
    },
    selectchangeHandler(falg) {
      if (falg) {
        if (this.organizeId && this.input4) {
          let data = { organizeId: this.organizeId }
          authorityApi.PostPort.OrganizationbyidgetDutyList(data, (res) => {
            if (res === 'no content') {
              this.PostList = []
            } else {
              this.PostList = res
            }
          })
        } else {
          this.PostList = []
          this.$message.error('请先选择机构')
        }
      } else {
        this.PostList = []
        // this.$message.error('请先选择机构')
      }
    },
    //岗位选择框发生改变触发
    changeSelectHandler(val) {
      this.degPageTypeDutyId = val
    },
    clicktree(e) {
      this.organizeId = e.id
      this.input4 = e.name
      this.degPageTypeName = e.name
      this.degPageTypeNameId = e.id
    },
    // 获取用户管理页面数据
    getUserRelease(list) {
      if (list && list.length > 0) {
        this.pageSize = list[0]
        this.currentPage = list[1]
      }
      let data = {
        linkUserName: this.linkUserName || '',
        userName: this.userName || '',
        organizationName: this.organizationName || '',
        pageSize: this.pageSize || list[0],
        currentPage: this.currentPage || list[1]
      }
      authorityApi.UserPort.page(data, (res) => {
        if (res.records === undefined) {
          this.tableDatarow = []
          this.total = 0
        } else {
          this.tableDatarow = res.records
          this.total = res.total
        }
      })
    },
    handleChange() {},
    //新建用户
    addUser() {
      this.editUserId = ''
      this.showUserDialog = true
    },
    //编辑用户
    updateUser(row) {
      this.editUserId = row.id
      this.showUserDialog = true
    },
    //删除用户
    logoutUser(row) {
      if (!row.id && this.tablerow.length === 0) {
        this.$message({
          type: 'error',
          message: '请选中需要删除的数据'
        })
      } else {
        this.$confirm('此操作将删除该用户, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          authorityApi.UserPort.deleteUser({ deleteFlag: 1, id: row.id, loading: true }, (res) => {
            this.$message({
              type: 'success',
              message: res
            })
            this.getUserRelease()
            this.$refs.pagecom.gobackone()
          })
        })
      }
    },
    //刷新按钮
    RefreshUser() {
      this.getUserRelease()
    },
    // 重置密码
    ResetPassword(row) {
      this.dialogVisibleUser = true
      this.userIDpsd = row.id
    },
    submitpssword(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let that = this
          let url = apiConfig.API.sysUserApi.resetUser
          let data = {
            id: this.userIDpsd,
            password: this.ruleForm.psd,
            loading: true
          }
          axiosUtil.postJson(url, data, function (res) {
            that.$refs.multipleTable.clearSelection()
            that.dialogVisibleUser = false
          })
        } else {
          return false
        }
      })
    },
    //当前行选中时变化
    tablerowchange(row) {
      this.tablerow = row
    },
    handleRemove(file, fileList) {},
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    //搜索用户事件
    serachResult() {
      this.getUserRelease()
    },
    resetResult() {
      this.linkUserName = ''
      this.userName = ''
      this.organizationName = ''
      this.getUserRelease()
    },
    //保存树形节点展开/收起状态
    expandHandler(data) {
      this.defaulitOpenList = getTreeObjHanlder(data, this.defaulitOpenList, true)
    },
    collapseHandler(data) {
      this.defaulitOpenList = getTreeObjHanlder(data, this.defaulitOpenList, false)
    }
  },
  watch: {
    input4(newval, oldval) {
      if (!newval) {
        this.dutyName = ''
      }
    },
    organizeId(newval) {
      if (!newval) {
        this.PostList = []
      }
    }
  }
}
</script>
<style lang="less" scoped>
.RootBox {
  background-color: #fff;
  height: 100%;
}
.iconlist {
  align-items: center;
}
.mbnav {
  background-color: #fff;
  .mbnavson {
    height: 51px;
    margin-left: 24px;
    line-height: 51px;
    border-bottom: 1px solid #4285f4;
  }
  .mbnavsontwo {
    margin-left: 24px;
    line-height: 64px;
    display: flex;
    align-items: center;
  }
  .mobile {
    line-height: 21px;
    margin-right: 25px;
  }
  .el-button--mini {
    font-size: 12px;

    // width: 72px;
  }
}
/deep/ .el-breadcrumb__separator[class*='icon'] {
  color: #4285f4;
}
/deep/.el-breadcrumb__inner {
  color: #4285f4 !important;
  font-size: 14px;
}
.tabbleList {
  background-color: #fff;
  height: 100%;
  display: flex;
}
/deep/.el-tree {
  height: 100%;
  width: 354px;
}
/deep/.el-table__row {
  height: 45px;
}
/deep/.el-tree-node__content {
  height: 45px;
  // background-color: red;
}
/deep/.el-tree-node__content:nth-child(add) {
  background-color: red;
}
/deep/.fatheritemlistname > .el-table--scrollable-x .el-table__body-wrapper {
  overflow-x: hidden !important;
}
.itemlistname {
  width: 100%;
  height: 45px;
  position: relative;
  // z-index: 2;
}
.fatheritemlistname {
  border-top: 1px solid #ccc !important;
  background-color: #fff !important;
  width: 354px !important;
  position: relative;
  display: flex;
  padding-top: 3.5px;
  padding-right: 20px;
}
.fatherform {
  width: 100%;
  padding-left: 20px;
  background-color: #fff !important;
}
/deep/.el-table__body {
  width: none !important;
}
/deep/ .el-table__header {
  width: none;
}
/deep/.cell {
  text-align: center;
}
/deep/ .el-dialog__title {
  color: #4285f4;
}
/deep/ .el-dialog {
  width: 75%;
}
/deep/ .el-dialog__header {
  border-bottom: 1px solid #4285f4;
}
/deep/.el-breadcrumb__inner {
  color: #4285f4 !important;
  font-size: 14px;
}
/deep/.el-breadcrumb__inner.is-link {
  color: #4285f4 !important;
  font-size: 14px !important ;
}
.dialog-footer {
  text-align: center;
}
.dialog-footer {
  text-align: center;
}

/deep/ .el-input {
  width: 200px;
}
/deep/ .el-input__inner {
  height: 33px;
}
/deep/ .el-card {
  // width: 40%;
  box-shadow: none !important;
}
.carld {
  width: 100%;
  display: flex;
  justify-content: center;
}
.formbtn {
  display: flex;
  height: 100%;
  margin: 0 30px;
  justify-content: center;
  flex-direction: column;
}
.gangwei {
  margin: 0 0 30px 0 !important;
}
.gangweistyle {
  color: #4285f4;
  font-size: 20px;
  text-align: center;
  font-weight: bold;
}
/deep/.clearfix {
  text-align: center;
}
/deep/.el-card__body {
  height: 400px;
}
.divbox {
  margin-top: 10px;
}
/deep/.is-leaf {
  background-color: none;
}
/deep/.el-table thead {
  background-color: #f6f6f6;
}
.search {
  margin-left: 24px;
}
.searchfather {
  display: flex;
  align-items: center;
  justify-content: flex-end;
}
.input-with-select {
  margin-left: 30px !important;
}
/deep/.el-pagination {
  margin-top: 20px;
  text-align: center;
}
/deep/ .el-pagination .el-pagination__jump .el-input {
  width: 50px !important;
}
/deep/.el-pagination {
  margin-bottom: 20px;
}
/deep/.el-select {
  margin-left: 30px;
}
/deep/.input-with-select {
  margin: 0;
}
/deep/.el-select .el-input .el-select__caret.is-reverse {
  transform: rotateZ(-180deg) translateY(-4px);
}
/deep/.el-select .el-input .el-select__caret {
  color: #c0c4cc;
  font-size: 14px;
  transition: transform 0.3s;
  transform: rotateZ(-90deg);
  /* transform: translateY(-4px); */
  cursor: pointer;
}
/deep/.el-tree .el-tree-node__children .el-tree-node__content {
  background-color: transparent !important;
}
.btnicon {
  font-size: 12px;
}
.addUser {
  background-color: #4285f4;
  color: #fff;
}
.updateUser {
  background-color: #1c84c6;
  color: #fff;
}
.Refresh {
  background-color: #f7a54a;
  color: #fff;
}
</style>
